package main.java;

import java.util.Random;
import java.util.Scanner;

/**
 * @Author
 * @Description
 * @Date 2022/6/16
 */
public class NGuessNumber {



    public static void main(String[] args) {
        int array[] = generateAnswer();
        while (true) {
            int array2[] = getPlayerInput();
            boolean isWin = isWin(array, array2);
            String alertStr = compareGuessAnswer(array, array2);
            System.out.println(alertStr);
            if (isWin) {
                Scanner scanner = new Scanner(System.in);
                System.out.println("输入y继续玩,输入n或其他值结束！");
                String s = scanner.next();
                if("y".equals(s)){
                    array = generateAnswer();
                }else{
                    break;
                }
            }
        }
    }





    /**
     * 随机生成 4位数字
     *
     * @return
     */
    public static int[] generateAnswer() {

        int i[] = new int[4];
        String str = "";
        Random random = new Random();
        int index = 0;
        while (true) {
            int num = random.nextInt(10);

            if (str.indexOf(num + "") > -1) {
                continue;
            }
            str += num + "";
            i[index] = num;
            index++;
            if (index == 4) {
                break;
            }
        }
        return i;
    }


    /**
     * 获取键盘输入 4 位数
     * @return
     */
    public static int[] getPlayerInput() {
        Scanner scanner = new Scanner(System.in);
        int array[] = new int[4];
        for (int i = 0; i < 4; i++) {
            System.out.println("请输入第" + (i + 1) + "个数");
            array[i] = scanner.nextInt();
        }
        return array;
    }
    /**
     *  判断后给出提示信息
     * @param array1  生成得
     * @param array2   输入得
     * @return
     */
    public static String compareGuessAnswer(int array1[], int array2[]) {

        String s1 = "";
        for (int i : array1) {
            s1 += i;
        }
        //System.out.println("系统生成得：" + s1);
        String s2 = "";
        for (int i : array2) {
            s2 += i;
        }
        if (s1.equals(s2)) {
            return "恭喜您猜对了!";
        }

        int containCorrect = 0;
        int locationCorrect = 0;

        for (int i = 0; i < array2.length; i++) {
            int ind = s1.indexOf(array2[i]+"");
            if (ind == -1) {
                continue;
            }
            containCorrect++;
            if (array1[i] == array2[i]) {
                locationCorrect++;
            }
        }
        return "您猜对了" + containCorrect + "位数, 位置正确得有" + locationCorrect + "位,请继续";
    }


    /**
     * 判断玩家是否赢
     * @param array1
     * @param array2
     * @return
     */
    public static boolean isWin(int array1[], int array2[]) {

        String s1 = "";
        for (int i : array1) {
            s1 += i;
        }
        String s2 = "";
        for (int i : array2) {
            s2 += i;
        }
        return s1.equals(s2);
    }







}
